# -*- coding: utf-8 -*-
from escpos.printer import Serial
from PIL import Image,ImageDraw,ImageFont,ImageOps
import six
import qrcode
import urllib2
import json
import time
def json_get():
    url='http://wms.firets.com/Warehouse/Label/get_json.html'   #页面的地址
    response = urllib2.urlopen(url)         #调用urllib2向服务器发送get请求
    return response.read()   #获取服务器返回的页面信息
class PrintJob:
    '''变量幅值'''
    def setdata(self):
        self.imgMode = "RGB"
        self.imgSize = (430,210)
        self.fontsize = 20
        self.bg_color = (255,255,255)
        self.fg_color = (0,0,0)
        self.logo =  Image.open('LOGO.png')
    '''打印任务类'''
    def sendpng(self):
        try:
            p = Serial("COM15",115200)
            #打印图片
            p.image("pr.png")
            #u移动到黑标
            p._raw(six.int2byte(29) + six.int2byte(12) + b'\x00')
            #p.colse()
        except ZeroDivisionError,e:
            print e.message
    def makeqr(self,text):
        data = "P/N:" + text
        img_file = r'py_qrcode.png'
        qr = qrcode.QRCode(
            version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=4,
            border=0
        )
        qr.add_data(data)
        qr.make(fit=True)
        self.qrimg = qr.make_image()
        self.qrimg.save(img_file)
    def makeimg(self,type=0,pn="P/N:56",sn="S/N:343434",quantity="0",model="asdfasf",name="asdfasf",text="aetsetrewtrwetr"):
        #创建一个空的图片
        self.img = Image.new(self.imgMode, self.imgSize, self.bg_color)
        #创建画刷，用来写文字到图片img上
        self.drawBrush = ImageDraw.Draw(self.img)
        #创建字体，fontFile为字体文件，若非系统字体需加详细路径
        self.fonta = ImageFont.truetype("msyh.ttc",24)
        #创建字体，fontFile为字体文件，若非系统字体需加详细路径
        self.fontb = ImageFont.truetype("msyh.ttc",18)
        top = 0
        if type == 1:
            self.drawBrush.text((0,top),'P/N: '+pn , fill=self.fg_color,font=self.fonta)
            top = top + 30
            #self.drawBrush.text((0,top),'Name:' , fill=self.fg_color,font=self.fontb)
            #top = top + 20
            if len(name)>0:
                self.drawBrush.text((0,top),name , fill=self.fg_color,font=self.fonta)
            top = top + 30
            self.drawBrush.text((0,top),'Model: '+model , fill=self.fg_color,font=self.fonta)
            top = top + 30
            if len(sn)>1:
                self.drawBrush.text((0,top),'S/N: '+sn , fill=self.fg_color,font=self.fonta)
                top = top + 30
            if quantity > 0:
                self.drawBrush.text((0,top),'Quantity: '+str(quantity) , fill=self.fg_color,font=self.fonta)
                top = top + 30
            if len(text)>0:
                self.drawBrush.text((0,top),text , fill=self.fg_color,font=self.fontb)
            self.img.paste(self.qrimg,(300,5))
            self.img.paste(self.logo,(300,120))
        else:
            self.fonta = ImageFont.truetype("msyh.ttc",120)
            self.drawBrush.text((20,10),pn , fill=self.fg_color,font=self.fonta)
        #保存图片
        self.img.save("pr.png", "png")
""" Seiko Epson Corp. Receipt Printer (EPSON TM-T88III) """
#p = Usb(0x04b8, 0x0202, 0, profile="TM-T88III")
#p = Serial("COM6",115200)
#p.text("Hello World\n")
#p.image("LOGO.png")
#p.barcode('1324354657687', 'EAN13', 64, 2, '', '')
#p.cut()
#u移动到黑标
#p._raw(six.int2byte(29) + six.int2byte(12) + b'\x00')

###

def runTask(): 
	while True:
		try:
			hr = json_get()
			j = json.loads(hr)
			print j['code']
			if j['code'] == 200:
				pj = PrintJob()
				pj.setdata()
				pj.makeqr(j['pn'])
				pj.makeimg(j['type'],j['pn'],j['sn'],j['quantity'],j['model'],j['name'],j['text'])
				pj.sendpng()
		except ZeroDivisionError,e:
			print e.message
		time.sleep(0.10)

runTask()

